﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

using Buddy.BehaviorTree;
using Buddy.Common;
using Buddy.CommonBot;
using Buddy.Common.Plugins;
using Buddy.Swtor;
using Buddy.Swtor.Objects;

using WingIt.Dynamics;
using Action = Buddy.BehaviorTree.Action;



namespace Buddywing.Plugins
{
    public class ChestinatorPlugin : IPlugin
    {
        #region Implementation of IEquatable<IPlugin>

        public bool Equals(IPlugin other)
        {
            return other.Name == Name;
        }

        #endregion

        #region Implementation of IPlugin

        public string Author { get { return "Neo93"; } }

        public Version Version { get { return new Version(1,0); } }

        public string Name { get { return "Chestinator"; } }

        public string Description { get { return "Moves to chests and opens them"; } }

        public Window DisplayWindow { get { return null; } }

        public TorPlaceable Chest { get { return ObjectManager.GetObjects<TorPlaceable>().FirstOrDefault(o => o.Name == "Security Chest"); } }

        private DateTime LastChecked { get; set; }

        private int count { get; set; }

        public void OnInitialize()
        {
            //Logging.Write("[Chestinator] Let's earn some $$$");

        }

        private void Log(string message)
        {
            Logging.WriteDiagnostic("[Chestinator] " + message);
        }

        /// <summary> Executes the pulse action. This is called every "tick" of the bot. </summary>
        public void OnPulse()
        {
            if (BuddyTor.Me.InCombat)
                return;

            Log("Running");
            if (DateTime.Now.Subtract(LastChecked).TotalSeconds > 10)
            {
                LastChecked = DateTime.Now;
                if (Chest != null)
                {
                    //TreeHooks.Instance.AddHook("TreeStart",
                    TreeHooks.Instance.InsertHook("TreeStart", 0,
                    new PrioritySelector(context => Chest,
                        new Decorator(ret => ret != null && !BuddyTor.Me.InCombat,
                            new PrioritySelector(
                                CommonBehaviors.MoveAndStop(ret => (ret as TorPlaceable).Position, 0.4f, true, "[Chestinator] Move to Chest"),
                                new Sequence(
                                    new Action(ret => (ret as TorPlaceable).Interact()),
                                    new Action(ret => count++),
                                    new Action(ret => Logging.Write("[Chestinator] Interacting with " + (ret as TorObject).Name + "(" + count + ")")),
                                    new WaitContinue(3, ret => Chest.IsLootable, null)
                                    )
                                )
                            )
                        )

                    );
                }

            }
        }

        /// <summary> Executes the initialize action. This is called at initial bot startup. (When the bot itself is started, not when Start() is called) </summary>


        /// <summary> Executes the shutdown action. This is called when the bot is shutting down. (Not when Stop() is called) </summary>
        public void OnShutdown()
        {
        }

        /// <summary> Executes the enabled action. This is called when the user has enabled this specific plugin via the GUI. </summary>
        public void OnEnabled()
        {
            Logging.Write("[Chestinator] Let's earn some $$$");
            /*TreeHooks.Instance.InsertHook("TreeStart", 0,
            new PrioritySelector(context => Chest,
                new Decorator(ret => ret != null && !BuddyTor.Me.InCombat,
                    new PrioritySelector(
                        CommonBehaviors.MoveAndStop(ret => (ret as TorPlaceable).Position, 0.4f, true, "[Chestinator] Move to Chest"),
                        new Sequence(
                            new Action(ret => (ret as TorPlaceable).Interact()),
                            new Action(ret => count++),
                            new Action(ret => Logging.Write("[Chestinator] Interacting with " + (ret as TorObject).Name + "(" + count + ")")),
                            new WaitContinue(10, ret => !Chest.IsLootable, null)
                            )
                        )
                    )
                )

            );*/
        }

        /// <summary> Executes the disabled action. This is called whent he user has disabled this specific plugin via the GUI. </summary>
        public void OnDisabled()
        {
            Logging.Write("[Chestinator] Y U NO WANT $$$?");
            //TreeHooks.Instance.RemoveHook("QuestProfileBehavior_Hook", 0);
        }

        #endregion
    }
}
